﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using System;
using System.Data;
using System.Data.SQLite;

namespace RabbitDownload.Helper.Enhance.SqlLite
{
    /**
     * 创建数据库文件直接创建，免费版不支持加密，别试了。
     * 创建 "CREATE TABLE t1(id varchar(4),score int)";
     * 主键自增表格"CREATE TABLE drticleinfo (Id INTEGER PRIMARY KEY AUTOINCREMENT,Title TEXT, Content TEXT )"
     * 插入 "INSERT INTO cat VALUES('casss',11,22)"
     */

    internal class SqlliteHelper
    {
        /// <summary>
        /// 数据库文件列表
        /// </summary>
        public static string DatabaseFloder = System.Environment.CurrentDirectory + "\\Bin\\DataPath\\";

        /// <summary>
        /// 初始化数据库列表
        /// </summary>
        public void InitDataBasePath()
        {
            FloderHelper.FloderExits(DatabaseFloder, true);
        }

        /// <summary>
        /// 查询数据库是否需要创建，需要创建 会在创建后返回true
        /// </summary>
        /// <param name="DataBaseName"></param>
        /// <param name="CreateFlag"></param>
        public bool ExitDabaBases(string DataBaseName, bool CreateFlag = true)
        {
            string DbSavePath = GetDbSavePath(DataBaseName);
            if (!FileHelper.FileExits(DbSavePath) && CreateFlag)
            {
                CreateDataBase(DataBaseName);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 创建sqllite数据库
        /// </summary>
        /// <param name="DataBaseName"></param>
        public void CreateDataBase(String DataBaseName)
        {
            InitDataBasePath();
            string DbSavePath = GetDbSavePath(DataBaseName);
            Console.WriteLine(DbSavePath);

            using (SQLiteConnection sQLiteConnection = ConnectionDataBases(DataBaseName))
            {
                sQLiteConnection.Open();
                sQLiteConnection.Dispose();
            }

            ///  SQLiteConnection.CreateFile (DbSavePath);
        }

        /// <summary>
        /// 获取数据库名字
        /// </summary>
        /// <param name="DataBaseName"></param>
        /// <returns></returns>
        private static string GetDbSavePath(string DataBaseName)
        {
            return string.Format(DatabaseFloder + "{0}.db", DataBaseName);
        }

        /// <summary>
        /// 链接数据库
        /// </summary>
        /// <param name="DataBaseName"></param>
        /// <returns></returns>
        public SQLiteConnection ConnectionDataBases(String DataBaseName)
        {
            SQLiteConnection cnn = new SQLiteConnection(string.Format("Data Source={0}", GetDbSavePath(DataBaseName)));

            return cnn;
        }

        /// <summary>
        /// 执行语句，失败返回-1
        /// </summary>
        /// <param name="DataBaseName"></param>
        /// <param name="ExecutionStatement">创建表语句</param>
        /// <returns></returns>
        public int ExecuteNonQuery(String DataBaseName, string ExecutionStatement)
        {
            return ExecuteNonQuery(ConnectionDataBases(DataBaseName), ExecutionStatement);
        }

        /// <summary>
        /// 执行语句，失败返回-1
        /// </summary>
        /// <param name="sQLiteConnection"></param>
        /// <param name="ExecutionStatement">创建表语句</param>
        /// <returns></returns>
        public int ExecuteNonQuery(SQLiteConnection sQLiteConnection, string ExecutionStatement)
        {
            int Code = -1;
            try
            {
                using (sQLiteConnection)
                {
                    sQLiteConnection.Open();
                    SQLiteCommand cmd = new SQLiteCommand();
                    cmd.Connection = sQLiteConnection;

                    //    cmd.CommandText = "CREATE TABLE t1(id varchar(4),score int)";
                    cmd.CommandText = ExecutionStatement;
                    Code = cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    sQLiteConnection.Close();
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Code;
        }

        /// <summary>
        /// 读取数据库返回结果
        /// </summary>
        /// <param name="DataBaseName"></param>
        /// <param name="ExecutionStatement"></param>
        /// <returns></returns>
        public DataTable ExecuteReader(String DataBaseName, string ExecutionStatement)
        {
            return ExecuteReader(ConnectionDataBases(DataBaseName), ExecutionStatement);
        }

        /// <summary>
        /// 读取数据库返回结果
        /// </summary>
        /// <param name="sQLiteConnection"></param>
        /// <param name="ExecutionStatement"></param>
        /// <returns></returns>
        public DataTable ExecuteReader(SQLiteConnection sQLiteConnection, string ExecutionStatement)
        {
            DataTable dataTable = new DataTable();
            try
            {
                using (sQLiteConnection)
                {
                    sQLiteConnection.Open();
                    SQLiteCommand cmd = new SQLiteCommand();
                    cmd.Connection = sQLiteConnection;
                    cmd.CommandText = ExecutionStatement;

                    using (SQLiteDataReader sQLiteDataReader = cmd.ExecuteReader())
                    {
                        if (sQLiteDataReader.HasRows)
                        {
                            dataTable.Load(sQLiteDataReader);
                        }
                        sQLiteDataReader.Dispose();
                    }
                    sQLiteConnection.Close();
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return dataTable;
        }
    }
}